package com.iterator.controller.wechat.lyh;

import com.iterator.bean.web.lyh.utils.BaseRespVO;
import com.iterator.bean.web.lyh.utils.DataCountDataVO;
import com.iterator.bean.wechat.lyh.WxCouponMyVO;
import com.iterator.service.wechat.lyh.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    //优惠券列表
    @RequestMapping("list")
    public BaseRespVO getCouponList(@RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size) {
        DataCountDataVO dataVO = wxCouponService.getCouponList(page, size);
        return BaseRespVO.ok(dataVO);
    }

    //我的优惠券列表
    ///wx/coupon/mylist?status=0&page=1&size=10
    @RequestMapping("mylist")
    public BaseRespVO getmyCouponList(@RequestParam("status") Integer status,
                                      @RequestParam("page") Integer page,
                                      @RequestParam("size") Integer size) {
        //获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        DataCountDataVO dataVO = wxCouponService.getMyCouponList(username, status, page, size);
        return BaseRespVO.ok(dataVO);
    }

    //当前订单可用优惠券列表
    //GET /wx/coupon/selectlist?cartId=0&grouponRulesId=0
    @RequestMapping("selectlist")
    public BaseRespVO selectList(@RequestParam("cartId") Integer cartId,
                                 @RequestParam("grouponRulesId") Integer grouponRulesId) {
        //获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        List<WxCouponMyVO> coupons = wxCouponService.selectList(username, cartId, grouponRulesId);
        return BaseRespVO.ok(coupons);
    }

    //优惠券领取
    //请求参数为 {"couponId":10} post
    @RequestMapping("receive")
    public BaseRespVO receiveCoupon(@RequestBody Map couponIdMap) {
        Integer couponId = (Integer) couponIdMap.get("couponId");
        //获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        Integer intCode = wxCouponService.receiveCoupon(username, couponId);
        if (intCode == null) {
            return BaseRespVO.fail(740, "优惠券已领完");
        }
        if (intCode == 0) {
            return BaseRespVO.fail(740, "优惠券已达领取上限");
        }
        return BaseRespVO.ok();
    }

    //优惠券兑换
    //请求参数为 {"code":"12356"} post
    @RequestMapping("exchange")
    public BaseRespVO exchangeCoupon(@RequestBody Map codeMap) {
        String code = (String) codeMap.get("code");
        //获得用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        Integer intCode = wxCouponService.exchangeCoupon(username, code);
        if (intCode == null) {
            return BaseRespVO.fail(742, "优惠券不正确");
        }
        if (intCode == 0) {
            return BaseRespVO.fail(742, "优惠券已达领取上限");
        }
        return BaseRespVO.ok();
    }
}
